package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    OrderSettingDao dao;


    @Override
    @Transactional
    public void addOrderSetting(List<OrderSetting> settingList) {

        if (settingList!=null&&settingList.size()>0){
            for (OrderSetting orderSetting : settingList) {
               OrderSetting orderSettingDb = dao.findOrderSettingByDate(orderSetting.getOrderDate());
            if (orderSettingDb == null){
                dao.add(orderSetting);
            }else {
                int reservations = orderSettingDb.getReservations();
                int number = orderSetting.getNumber();
                if (reservations>number){
                    throw new RuntimeException("已预约人数大于可预约人数,修改不了");
                }else {
                    dao.edit(orderSetting);
                }
            }
            }

        }

    }

    @Override
    public List<OrderSetting> findBymonth(String month) {
        String firstDay = month+"-01";
        String lastDay = month+"-31";
       List<OrderSetting> orderSettingList =  dao.findBymonth(firstDay,lastDay);
//        System.out.println(orderSettingList);
        return orderSettingList;
    }
}
